var gulp = require('gulp');
var webpack = require('gulp-webpack');
var named = require('vinyl-named');
var fs = require('fs');
var del = require('del');

var appList = ['app'];


gulp.task('default', ['build'], function() {
	console.log('done');
})

gulp.task('build', function() {
	del([
		'build/app/*',
	]);	
	return gulp.src(mapFiles(appList, 'js'))
		.pipe(named())
		.pipe(webpack(require("./webpack.production.config.js")))
		.pipe(gulp.dest('build/app/'))
});


gulp.task('watch', function() {
	return gulp.src(mapFiles(appList, 'js'))
		.pipe(named())
		.pipe(webpack(require("./webpack.dev.web.config.js")))
		.pipe(gulp.dest('build/'))
});

/**
 * @private
 * 因为目前使用导入webpack.config.js的方式,故此方法废弃
 */
/*function getConfig(opt) {
	var config = {
		module: {
			loaders: [{
				test: /\.vue$/,
				loader: 'vue'
			}, {
				test: /\.css$/,
				loader: 'style!css!autoprefixer?sourceMap'
			}, {
				test: /\.scss$/,
				loader: 'style!css!sass?sourceMap'
			}]
		},
		devtool: 'source-map'
	}
	if (!opt) {
		return config
	}
	for (var i in opt) {
		config[i] = opt[i]
	}
	return config
}*/

/**
 * @private
 */
function mapFiles(list, extname) {
	return list.map(function(app) {
		return 'src/' + app + '.' + extname
	})
}